package com.ruoyi.app.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.app.domain.DebtRecordsFile;
import com.ruoyi.app.domain.vo.DebtRecordsVo;
import com.ruoyi.app.domain.vo.RestockInfoVo;
import com.ruoyi.app.enums.DebtRecordsStatusEnum;
import com.ruoyi.app.enums.DebtRecordsTypeEnum;
import com.ruoyi.app.service.IDebtRecordsFileService;
import com.ruoyi.common.exception.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.app.mapper.DebtRecordsMapper;
import com.ruoyi.app.domain.DebtRecords;
import com.ruoyi.app.service.IDebtRecordsService;
import org.springframework.util.CollectionUtils;

/**
 * 供应商信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-29
 */
@Service
public class DebtRecordsServiceImpl extends ServiceImpl<DebtRecordsMapper, DebtRecords> implements IDebtRecordsService
{
    @Autowired
    private DebtRecordsMapper debtRecordsMapper;

    @Autowired
    IDebtRecordsFileService debtRecordsFileService;

    /**
     * 查询供应商信息
     * 
     * @param id 供应商信息主键
     * @return 供应商信息
     */
    @Override
    public DebtRecordsVo selectDebtRecordsById(String id)
    {
        DebtRecords debtRecords = baseMapper.selectById(id);
        if(debtRecords == null){
            return null;
        }
        DebtRecordsVo debtRecordsVo = new DebtRecordsVo();
        BeanUtils.copyProperties(debtRecords,debtRecordsVo);
        debtRecordsVo.setDebtRecordsFiles(debtRecordsFileService.selectByRecordsId(debtRecords.getId()));
        return debtRecordsVo;
    }

    /**
     * 查询供应商信息列表
     * 
     * @param debtRecordsVo 供应商信息
     * @return 供应商信息
     */
    @Override
    public List<DebtRecordsVo> selectDebtRecordsList(DebtRecordsVo debtRecordsVo)
    {
        LambdaQueryWrapper<DebtRecords> lambdaQueryWrapper = new LambdaQueryWrapper<>(debtRecordsVo);
        lambdaQueryWrapper.eq(DebtRecords::getShopId,debtRecordsVo.getShopId());
        lambdaQueryWrapper.orderByDesc(DebtRecords::getCreateTime);
        List<DebtRecords> debtRecords = baseMapper.selectList(lambdaQueryWrapper);
        if(CollectionUtils.isEmpty(debtRecords)){
            return new ArrayList<>();
        }
        List<DebtRecordsVo> debtRecordsVos = JSONArray.parseArray(JSONArray.toJSONString(debtRecords), DebtRecordsVo.class);
        return debtRecordsVos;
    }

    /**
     * 添加或更新
     * @param debtRecordsVo
     * @return
     */
    @Override
    public String add(DebtRecordsVo debtRecordsVo) {
        if(debtRecordsVo == null){
            throw new ServiceException("添加内容不能为空");
        }
        String type = debtRecordsVo.getType();
        if(StringUtils.isBlank(type)){
            throw new ServiceException("添加类型不能为空");
        }
        DebtRecordsTypeEnum byType = DebtRecordsTypeEnum.getByType(type);
        if(null == byType){
            throw new ServiceException("添加类型不存在");
        }
        debtRecordsVo.setTypeTxt(byType.getName());
        DebtRecordsStatusEnum notReturned = DebtRecordsStatusEnum.NOT_RETURNED;
        debtRecordsVo.setStatus(notReturned.getType());
        debtRecordsVo.setStatusTxt(notReturned.getName());
        boolean b = saveOrUpdate(debtRecordsVo);
        // 清除文件
        debtRecordsFileService.deleteByRecordsId(debtRecordsVo.getId());
        // 添加文件
        List<DebtRecordsFile> debtRecordsFiles = debtRecordsVo.getDebtRecordsFiles();
        AtomicInteger sort = new AtomicInteger(1);
        if(!CollectionUtils.isEmpty(debtRecordsFiles)){
            debtRecordsFiles.forEach(item -> {
                item.setShopId(debtRecordsVo.getShopId());
                item.setDebtRecordsId(debtRecordsVo.getId());
                item.setSort(sort.getAndIncrement());
            });
            debtRecordsFileService.saveBatch(debtRecordsFiles);
        }
        return null;
    }


    /**
     * 本月借钱款统计
     * @param shopId
     * @return
     */
    @Override
    public List<DebtRecordsVo> currentMonthStatistics(String shopId) {
        if(StringUtils.isBlank(shopId)){
            return null;
        }
        return baseMapper.currentMonthStatistics(shopId, DebtRecordsStatusEnum.NOT_RETURNED.getType());
    }

}
